'''
version: V0.1
Author: xubing
Date: 2020-12-01 15:14:23
LastEditors: xubing
LastEditTime: 2020-12-09 15:37:08
Description: 产生一些数据字典的映射
'''
import pandas as pd
from conf import config
import pickle
import os


class Mapper:
    def __init__(self) -> None:
        super().__init__()
        if not os.path.exists('assets/middle'):
            os.mkdir('assets/middle')

    def gen_code2name_pkl(self):
        # 产生code到name的映射pkl文件
        # 编码到中文名的映射
        feature_desc = pd.read_excel(config.feature_lib, sheet_name='企业-明细')
        feature_desc_supplement = pd.read_excel(
            config.feature_lib, sheet_name='企业-属性-明细')

        feature_desc_filted = feature_desc[['标签编码', '变量名']]
        feature_desc_filted.rename(columns={'标签编码': '特征编码'}, inplace=True)

        feature_desc_supplement_filted = feature_desc_supplement[[
            '属性编码', '变量名']]
        feature_desc_supplement_filted.rename(
            columns={'属性编码': '特征编码'}, inplace=True)

        feature_desc_all = pd.concat(
            [feature_desc_filted, feature_desc_supplement_filted])
        code2name = {
            k: v
            for k, v in zip(feature_desc_all['特征编码'].tolist(), feature_desc_all['变量名'].tolist())
        }

        with open(config.code2name, 'wb') as f:
            pickle.dump(code2name, f)

    def gen_all_pkl(self):
        self.gen_code2name_pkl()

    @classmethod
    def get_code2name_mapper(cls):
        with open(config.code2name, 'rb') as f:
            code2name = pickle.load(f)
        return code2name


if __name__ == '__main__':
    mapper = Mapper()
    mapper.gen_all_pkl()
    code2name = mapper.get_code2name_mapper()
    print(code2name)
    # mapper.gen_all_pkl()
    # r2s = Mapper.get_rating2score_mapper()
    # print(r2s)
